﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;


public partial class Voorwerpen : System.Web.UI.Page
{
    DB db = new DB();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["Username"] == null)
        {
            Response.Redirect("Default.aspx");
        }
        if (!IsPostBack)
        {
            BindData();
        }
    }


    //GridView Gevonden
    public void BindData()
    {
        string sql = string.Format("Select * from Voorwerpen");
        SqlDataAdapter da = new SqlDataAdapter(sql, db.GetConnection());
        DataTable dt = new DataTable();
        da.Fill(dt);
        Grid.DataSource = dt;
        Grid.DataBind();
    }


    protected void Grid_RowEditing(object sender, GridViewEditEventArgs e)
    {
        Grid.EditIndex = e.NewEditIndex;
        BindData();
    }
    protected void Grid_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        Grid.EditIndex = -1;
        BindData();
    }


    protected void Grid_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {

        if (((LinkButton)Grid.Rows[0].Cells[0].Controls[0]).Text == "Maak")
        {
            SqlConnection con = db.GetConnection();
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "INSERT INTO Voorwerpen(Naam,Omschrijving,Datum,Status) VALUES(@name,@omschrijving,@datum,@status)";
            cmd.Parameters.Add("@name", SqlDbType.VarChar).Value = ((TextBox)Grid.Rows[0].Cells[2].Controls[0]).Text;
            cmd.Parameters.Add("@datum", SqlDbType.VarChar).Value = ((TextBox)Grid.Rows[0].Cells[3].Controls[0]).Text;
            cmd.Parameters.Add("@omschrijving", SqlDbType.VarChar).Value = ((TextBox)Grid.Rows[0].Cells[4].Controls[0]).Text;
            cmd.Parameters.Add("@status", SqlDbType.VarChar).Value = ((TextBox)Grid.Rows[0].Cells[5].Controls[0]).Text;
            cmd.Connection = con;
            con.Open();

            string status = ((TextBox)Grid.Rows[e.RowIndex].Cells[5].Controls[0]).Text;
            if (status.Equals("gevonden"))
            {
                cmd.ExecuteNonQuery();
                con.Close();
                Grid.EditIndex = -1;
                BindData();
            }
            else if (status.Equals("verloren"))
            {
                cmd.ExecuteNonQuery();
                con.Close();
                Grid.EditIndex = -1;
                BindData();
            }
            else
            {
                ((TextBox)Grid.Rows[e.RowIndex].Cells[5].Controls[0]).Text = "Vul gevonden/verloren in";
                ((TextBox)Grid.Rows[e.RowIndex].Cells[5].Controls[0]).ForeColor = System.Drawing.Color.Red;
            }
        }
        else
        {
            SqlConnection con = db.GetConnection();
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "UPDATE Voorwerpen SET Naam=@name,Datum=@datum,Omschrijving=@omschrijving,Status=@status WHERE ItemID=@id";
            cmd.Parameters.Add("@name", SqlDbType.VarChar).Value = ((TextBox)Grid.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
            cmd.Parameters.Add("@datum", SqlDbType.VarChar).Value = ((TextBox)Grid.Rows[e.RowIndex].Cells[3].Controls[0]).Text;
            cmd.Parameters.Add("@omschrijving", SqlDbType.VarChar).Value = ((TextBox)Grid.Rows[e.RowIndex].Cells[4].Controls[0]).Text;
            cmd.Parameters.Add("@id", SqlDbType.Int).Value = Convert.ToInt32(Grid.Rows[e.RowIndex].Cells[1].Text);
            cmd.Parameters.Add("@status", SqlDbType.VarChar).Value = ((TextBox)Grid.Rows[e.RowIndex].Cells[5].Controls[0]).Text;

            cmd.Connection = con;
            con.Open();

            string status = ((TextBox)Grid.Rows[e.RowIndex].Cells[5].Controls[0]).Text;
            if (status.Equals("gevonden"))
            {
                cmd.ExecuteNonQuery();
                con.Close();
                Grid.EditIndex = -1;
                BindData();
            }
            else if (status.Equals("verloren"))
            {
                cmd.ExecuteNonQuery();
                con.Close();
                Grid.EditIndex = -1;
                BindData();
            }
            else
            {
                ((TextBox)Grid.Rows[e.RowIndex].Cells[5].Controls[0]).Text = "Vul gevonden/verloren in";
                ((TextBox)Grid.Rows[e.RowIndex].Cells[5].Controls[0]).ForeColor = System.Drawing.Color.Red;
            }

        }

    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        SqlConnection con = db.GetConnection();
        SqlDataAdapter da = new SqlDataAdapter("SELECT ItemID, Naam,Datum,Omschrijving,Status FROM Voorwerpen", con);
        DataTable dt = new DataTable();
        da.Fill(dt);

        // Here we'll add a blank row to the returned DataTable
        DataRow dr = dt.NewRow();
        dt.Rows.InsertAt(dr, 0);
        //Creating the first row of GridView to be Editable
        Grid.EditIndex = 0;
        Grid.DataSource = dt;
        Grid.DataBind();
        //Changing the Text for Inserting a New Record
        ((LinkButton)Grid.Rows[0].Cells[0].Controls[0]).Text = "Maak";

    }

    protected void btnZoek_Click(object sender, EventArgs e)
    {
        DataTable dt = db.getZoekOpdracht(txtZoek.Text);

        Grid.DataSource = dt;
        Grid.DataBind();

    }



    protected void btnRefresh_Click(object sender, EventArgs e)
    {
        BindData();
    }
    protected void Grid_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        SqlConnection con = db.GetConnection();
        SqlCommand cmd = new SqlCommand();
        cmd.CommandText = "DELETE FROM Voorwerpen WHERE ItemID=@id";
        cmd.Parameters.Add("@id", SqlDbType.Int).Value = Convert.ToInt32(Grid.Rows[e.RowIndex].Cells[1].Text);

        cmd.Connection = con;
        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();

        BindData();

    }

}